<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of settings
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Settings extends MY_Controller {

    //put your code here


    function settings() {
        parent :: __construct();
        $this->template->set_theme('admin');
        $this->template->set_layout('admin_layout');
        $this->commoncall->check_admin_login();
    }

    function index() {
        $data['settings'] = $this->settings_model->settings_detail();

        $this->template->title('Settings', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_site_settings', $data);
    }

    function get_setting() {
        $data['settings'] = $this->settings_model->settings_detail();
        if ($this->input->post('get_value')) {
            $this->form_validation->set_rules('setting_variable', 'setting_variable', 'required|xss_clean');
            $name = $this->input->post('setting_variable');
            if ($this->form_validation->run()) {
                $this->db->select('*')->from('web_settings')->where('variable', $name);
                $query = $this->db->get();
                $result = $query->result();
                $row = $query->row();
                $data['setting_id'] = $row->id;
                $data['setting_description'] = $row->description;
                $data['setting_variable'] = $row->variable;
                $data['setting_value'] = $row->value;
                $data['setting_auto'] = $row->auto;
            } else {
                $this->session->set_flashdata('message', 'Choose Settings type');
                redirect('admin/settings', $data);
            }
        }

        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_site_settings', $data);
    }

    function setting_update() {

        if ($this->input->post('setting_update')) {
            $this->form_validation->set_rules('variable_value', 'variable_value', 'required|xss_clean');
            $this->form_validation->set_rules('setting_variable', 'setting_variable', 'required|xss_clean');
            $this->form_validation->set_rules('setting_id', 'setting_id', 'required|xss_clean');
            $id = trim($this->input->post('setting_id'));
            $setting_value = trim($this->input->post('variable_value'));

            if ($this->form_validation->run()) {
                $data = array('value' => $setting_value);
                $this->db->where('id', $id);
                $this->db->update('web_settings', $data);
            }
        }

        redirect('admin/settings');
    }

    function password() {
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_password_change');
    }

    function pass_change() {

        if ($this->input->post('verify')) {
            $this->form_validation->set_rules('oldPass', 'oldPass', 'required|xss_clean');
            $this->form_validation->set_rules('newPass', 'newPass', 'required|xss_clean ');
            $this->form_validation->set_rules('repeatPass', 'repeatPass', 'required|xss_clean|matches[newPass]');
            if ($this->form_validation->run()) {
                if ($this->session->userdata('sess_admin_password') == trim($this->input->post('oldPass'))) {
                    $data = array('password' => $this->input->post('newPass'));
                    $this->db->where('name', $this->session->userdata('sess_admin_name'));
                    $this->db->update('admin_user', $data);
                    $this->session->set_flashdata('message', 'Password Changed Sucessfully Login with new Password....');
                    redirect('admin/settings/password');
                } else {
                    $this->session->set_flashdata('message', 'Old password does not match or empty');
                    redirect('admin/settings/password');
                }
            } else {
                $this->session->set_flashdata('message', 'Error!Fields are empty or repeat password mismatch ');
                redirect('admin/settings/password');
            }
        }
    }

    function sitelogo() {
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_site_logo');
    }

    public function do_upload() {

        if ($_FILES['userfile']['error'] == 0) {
            $ext = preg_replace("/.*\.([^.]+)$/", "\\1", $_FILES['userfile']['name']);
            //$filename= "sitelogo.".$ext;
            $filename = 'sitelogo.gif';

            $config['file_name'] = $filename;
            $config['upload_path'] = './assets/images/';
            $config['allowed_types'] = 'gif|image/gif';
            $config['overwrite'] = true;
            $config['remove_spaces'] = true;
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('userfile')) {
                $this->session->set_flashdata('message', $this->upload->display_errors('<p class="error">', '</p>'));
            }
            redirect('admin/sitelogo');
        } else {
            $this->session->set_flashdata('message', 'Please Choose a correct file');
            redirect('admin/sitelogo');
        }
    }

}

?>